<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
    width="30%">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
      <el-form-item label="服务员工号" prop="no">
        <el-input v-model="dataForm.no" placeholder="服务员工号"></el-input>
      </el-form-item>
    <el-form-item label="服务员姓名" prop="name">
      <el-input v-model="dataForm.name" placeholder="服务员姓名"></el-input>
    </el-form-item>
    <el-form-item label="性别" prop="sex">
        <el-select v-model="dataForm.sex" placeholder="请选择">
          <el-option
            v-for="item in sexlist"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
    </el-form-item>
    <el-form-item label="状态" prop="status">
      <el-select v-model="dataForm.status" placeholder="请选择">
        <el-option
          v-for="item in statuslist"
          :key="item.value"
          :label="item.label"
          :value="item.value">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="备注" prop="remakes">
      <el-input v-model="dataForm.remakes" placeholder="备注"></el-input>
    </el-form-item>

    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        sexlist:[{
          value: '1',
          label: '女'
        }, {
          value: '0',
          label: '男'
        }],
        statuslist:[{
          value: '1',
          label: '有效'
        }, {
          value: '0',
          label: '无效'
        }],
        dataForm: {
          id: 0,
          name: '',
          sex: '',
          status: '',
          remakes: '',
          sellerId: '',
          creatTime: '',
          creatUerId: '',
          no: ''
        },
        dataRule: {
          name: [
            { required: true, message: '服务员姓名不能为空', trigger: 'blur' }
          ],
          sex: [
            { required: true, message: '性别不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '状态不能为空', trigger: 'blur' }
          ],
         
          no: [
            { required: true, message: '服务员工号不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/hotel/twaiter/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.name = data.tWaiter.name
                this.dataForm.sex = data.tWaiter.sex
                this.dataForm.status = data.tWaiter.status
                this.dataForm.remakes = data.tWaiter.remakes
                this.dataForm.sellerId = data.tWaiter.sellerId
                this.dataForm.creatTime = data.tWaiter.creatTime
                this.dataForm.creatUerId = data.tWaiter.creatUerId
                this.dataForm.no = data.tWaiter.no
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/hotel/twaiter/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'name': this.dataForm.name,
                'sex': this.dataForm.sex,
                'status': this.dataForm.status,
                'remakes': this.dataForm.remakes,
                'sellerId': this.dataForm.sellerId,
                'creatTime': this.dataForm.creatTime,
                'creatUerId': this.dataForm.creatUerId,
                'no': this.dataForm.no
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
